package com.dong.rabbitmq.ackwork;

import com.dong.rabbitmq.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

import java.util.Scanner;

/**
 * 生产者
 *
 * @author dongjian
 * 消息在手动应答时不丢失，放回队列重新消费
 */
public class TaskAckProducer01 {

    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtil.getChannel();

        //声明队列 durable=true队列持久化，mq服务器重启依然存在（重新声明会报错，需要先将原来的队列删除）
        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

        // 控制台输入
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String msg = scanner.next();
            // MessageProperties.PERSISTENT_TEXT_PLAIN:消息持久化
            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes("UTF-8"));
            System.out.println("消息发送完毕" + msg);
        }
    }
}
